package com.zkca.xf.ui.activity;


import android.Manifest;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.tbruyelle.rxpermissions2.RxPermissions;
import com.zkca.xf.Constants_SP;
import com.zkca.xf.R;
import com.zkca.xf.base.BaseTitleActivity;
import com.zkca.xf.model.javabean.UserId;
import com.zkca.xf.presenter.LoginPresenter;
import com.zkca.xf.ui.activity.me.ChangeQasswordActivity;
import com.zkca.xf.utils.AppUtils;
import com.zkca.xf.utils.DES3;
import com.zkca.xf.utils.LogUtil;
import com.zkca.xf.utils.SPUtil;
import com.zkca.xf.utils.ToastUtils;
import com.zkca.xf.view.LoginView;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.functions.Consumer;


public class Activity_Login extends BaseTitleActivity implements LoginView {

    @BindView(R.id.editText_name)
    EditText editTextName;
    @BindView(R.id.line_user)
    LinearLayout lineUser;
    @BindView(R.id.text_fenge)
    TextView textFenge;
    @BindView(R.id.editText_psw)
    EditText editTextPsw;
    @BindView(R.id.relativeLayout)
    RelativeLayout relativeLayout;
    @BindView(R.id.login)
    Button login;
    private Map<String, String> map;
    private LoginPresenter loginPresenter;
    private RxPermissions rxPermissions;
    String[] permissions = {Manifest.permission.READ_PHONE_STATE};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lgin);
        ButterKnife.bind(this);
        loginPresenter = new LoginPresenter(this, this);

        initview();
    }

    private void initview() {
        setTitleText("登陆");
        map = new HashMap();
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                initdata();
            }
        });
    }


    @Override
    public void setTitleText(String c) {
        super.setTitleText(c);
    }

    private void initdata() {
        final String name = editTextName.getText().toString();
        final String psw = editTextPsw.getText().toString();
        if (TextUtils.isEmpty(name)) {
            ToastUtils.show(Activity_Login.this, "请输入用户名");
            return;
        }
        if (TextUtils.isEmpty(psw)) {
            ToastUtils.show(Activity_Login.this, "请输入密码");
            return;
        }
        try {
            rxPermissions = new RxPermissions(this);
            rxPermissions.request(permissions)
                    .subscribe(new Consumer<Boolean>() {
                        @Override
                        public void accept(@NonNull Boolean granted) throws Exception {
                            if (granted) {//同意授权
//                                TelephonyManager telephonyManager = (TelephonyManager) Activity_Login.this.getSystemService(Activity_Login.this.TELEPHONY_SERVICE);
//                                @SuppressLint("MissingPermission") String imei = telephonyManager.getDeviceId();
//
                                String imei = AppUtils.getIMEI();
                                LogUtil.e("lcq", "imei=" + imei);
                                String userPsw = new DES3().encode(psw);
                                map.put("user_mobile", name);
                                map.put("user_pwd", userPsw);
                                map.put("s4", imei);
                                map.put("s5", "3");
                                loginPresenter.Login(map);
                            } else {//拒绝
                                ToastUtils.show(Activity_Login.this, "请开启手机权限");
                            }
                        }
                    });
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


    @Override
    public void showProgressDialog() {

    }

    @Override
    public void hideProgressDialog() {

    }


    @Override
    public void showError(String msg) {

    }

    @Override
    public void getUserId(UserId userId) {
        if(userId.getStatus()==1){
            String user_id = userId.getUserId();
            String orgId = userId.getOrgId();

            SPUtil.putString(this,Constants_SP.USER_ID, user_id);
            SPUtil.putString(this,Constants_SP.ORG_ID, orgId);
            gotoActivity(MainActivity.class,true);
        }else{
            ToastUtils.show(Activity_Login.this,userId.getInfo());
        }
    }


}
